#!/usr/bin/env bash

#   This file is a part of Neutrino - a lightweight UI library accelerated with OpenGL
#   Copyright (C) 2023 bsolos
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.

out_c="$1"
shift

out_h="$1"
shift

wd=`pwd`

rm -f "${out_c}"
rm -f "${out_h}"

base_h=`basename "${out_h}"`
echo "#include <${base_h}>" >> "${out_c}"
echo "#include <stddef.h>" >> "${out_h}"

for arg in "$@"
do
    varname=`realpath --relative-to="${wd}/.." "${arg}"`
    varname=`printf "__${varname}" | sed "s/[^a-zA-Z0-9]/_/g"`
    
    hexdump=`cat "${arg}" | xxd -i | tr "\\n" " " | sed "s/\\s//g"`
    hexdump="${hexdump},0x00"

    cvars="unsigned char ${varname}_arr[]={${hexdump}};unsigned char *${varname}=${varname}_arr;size_t ${varname}_len=$((${#hexdump}/5));"
    printf "${cvars}" >> "${out_c}"

    hvars="extern unsigned char *${varname};extern size_t ${varname}_len;"
    printf "${hvars}" >> "${out_h}"
done
